package com.ruoyi.zsyh.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.zsyh.domain.ZsyhAgentCooperateRel;
import com.ruoyi.zsyh.service.IZsyhAgentCooperateRelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhAgentCooperate;
import com.ruoyi.zsyh.service.IZsyhAgentCooperateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大区会长产品合作Controller
 * 
 * @author yuexl
 * @date 2024-10-15
 */
@Controller
@RequestMapping("/zsyh/agentCooperate")
public class ZsyhAgentCooperateController extends BaseController
{
    private String prefix = "zsyh/agentCooperate";

    @Autowired
    private IZsyhAgentCooperateService zsyhAgentCooperateService;
    @Autowired
    private IZsyhAgentCooperateRelService zsyhAgentCooperateRelService;

    @RequiresPermissions("zsyh:agentCooperate:view")
    @GetMapping()
    public String agentCooperate()
    {
        return prefix + "/agentCooperate";
    }

    @GetMapping("/reviewAgentCooperate/{id}")
    public String reviewAgentCooperate(@PathVariable("id") String id, ModelMap mmap) {
        ZsyhAgentCooperate zsyhAgentCooperate = zsyhAgentCooperateService.selectZsyhAgentCooperateById(id);
        mmap.put("zsyhAgentCooperate", zsyhAgentCooperate);
        return prefix + "/reviewAgentCooperate";
    }
    /**
     * 查询大区会长产品合作列表
     */
    @RequiresPermissions("zsyh:agentCooperate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        startPage();
        List<ZsyhAgentCooperate> list = zsyhAgentCooperateService.selectZsyhAgentCooperateList(zsyhAgentCooperate);
        return getDataTable(list);
    }

    /**
     * 查询大区会长产品合作列表
     */
    @PostMapping("/listChild")
    @ResponseBody
    public TableDataInfo listChild(String cooperateId)
    {
        startPage();
        ZsyhAgentCooperateRel zsyhAgentCooperateRel = new ZsyhAgentCooperateRel();
        zsyhAgentCooperateRel.setCooperateId(cooperateId);
        List<ZsyhAgentCooperateRel> list = zsyhAgentCooperateRelService.selectZsyhAgentCooperateRelList(zsyhAgentCooperateRel);
        return getDataTable(list);
    }

    /**
     * 导出大区会长产品合作列表
     */
    @RequiresPermissions("zsyh:agentCooperate:export")
    @Log(title = "大区会长产品合作", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        List<ZsyhAgentCooperate> list = zsyhAgentCooperateService.selectZsyhAgentCooperateList(zsyhAgentCooperate);
        ExcelUtil<ZsyhAgentCooperate> util = new ExcelUtil<ZsyhAgentCooperate>(ZsyhAgentCooperate.class);
        return util.exportExcel(list, "大区会长产品合作数据");
    }

    /**
     * 新增大区会长产品合作
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存大区会长产品合作
     */
    @RequiresPermissions("zsyh:agentCooperate:add")
    @Log(title = "大区会长产品合作", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        return toAjax(zsyhAgentCooperateService.insertZsyhAgentCooperate(zsyhAgentCooperate));
    }

    /**
     * 修改大区会长产品合作
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAgentCooperate zsyhAgentCooperate = zsyhAgentCooperateService.selectZsyhAgentCooperateById(id);
        mmap.put("zsyhAgentCooperate", zsyhAgentCooperate);
        return prefix + "/edit";
    }

    /**
     * 修改保存大区会长产品合作
     */
    @RequiresPermissions("zsyh:agentCooperate:edit")
    @Log(title = "大区会长产品合作", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        zsyhAgentCooperate.setOperateName(ShiroUtils.getSysUser().getUserName());
        return toAjax(zsyhAgentCooperateService.updateZsyhAgentCooperateEasy(zsyhAgentCooperate));
    }

    /**
     * 删除大区会长产品合作
     */
    @RequiresPermissions("zsyh:agentCooperate:remove")
    @Log(title = "大区会长产品合作", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zsyhAgentCooperateService.deleteZsyhAgentCooperateByIds(ids));
    }

    @PostMapping("/getSumEarnestMoney")
    @ResponseBody
    public AjaxResult getSumEarnestMoney(ZsyhAgentCooperate zsyhAgentCooperate)
    {
        startPage();
        Map<String, Object> params = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(EmptyUtils.isNotEmpty(zsyhAgentCooperate.getBeginCreateTime())){
            params.put("beginCreateTime",sdf.format(zsyhAgentCooperate.getBeginCreateTime()));
        }
        if(EmptyUtils.isNotEmpty(zsyhAgentCooperate.getEndCreateTime())){
            params.put("endCreateTime",sdf.format(zsyhAgentCooperate.getEndCreateTime()));
        }
        zsyhAgentCooperate.setParams(params);
        BigDecimal sumEarnestMoney = new BigDecimal(0.0);
        List<ZsyhAgentCooperate> list1 = zsyhAgentCooperateService.selectZsyhAgentCooperateList(zsyhAgentCooperate);
        for(ZsyhAgentCooperate temp : list1){
            sumEarnestMoney = sumEarnestMoney.add(temp.getEarnestMoney().setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        return new AjaxResult(AjaxResult.Type.SUCCESS,sumEarnestMoney.toString());
    }

}
